package com.ukao.cashregister.ui.createOrders;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.ukao.cashregister.R;
import com.ukao.cashregister.base.MvpFragment;
import com.ukao.cashregister.bean.ChooseTag;
import com.ukao.cashregister.bean.ClientGroupingBean;
import com.ukao.cashregister.bean.ClothginQuiryBean;
import com.ukao.cashregister.bean.Coupons;
import com.ukao.cashregister.bean.RechargeBean;
import com.ukao.cashregister.bean.UserInfoBean;
import com.ukao.cashregister.consts.GlobalConsts;
import com.ukao.cashregister.dialog.BindingDialogFragment;
import com.ukao.cashregister.dialog.ChooseTagDialogFragment;
import com.ukao.cashregister.dialog.ClientGroupingDialogFragment;
import com.ukao.cashregister.dialog.CouponsDialogFragment;
import com.ukao.cashregister.dialog.DialogTool;
import com.ukao.cashregister.dialog.EditNameDialogFragment;
import com.ukao.cashregister.dialog.EditSexDialogFragment;
import com.ukao.cashregister.dialog.VipCardDialogFragment;
import com.ukao.cashregister.eventbus.MembershipCardEvent;
import com.ukao.cashregister.eventbus.UserInfoEvent;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.pesenter.UserInfoPesenter;
import com.ukao.cashregister.rfid.MembershipCardHelper;
import com.ukao.cashregister.ui.clothinQuery.ClothingDetailsFragment;
import com.ukao.cashregister.ui.pickClothing.PickClothingUserInfoRightFragment;
import com.ukao.cashregister.ui.stokin.GatheringFragment;
import com.ukao.cashregister.ui.vipManager.VipDetailsTabFragment;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.DensityUtils;
import com.ukao.cashregister.utils.ImageUtils;
import com.ukao.cashregister.utils.L;
import com.ukao.cashregister.utils.MyDateUtils;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.utils.ThreadUtils;
import com.ukao.cashregister.view.UserInfoView;
import com.ukao.cashregister.widget.ShapedImageView;

import org.greenrobot.eventbus.Subscribe;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.cashregister.R.id.user_info_balance;
import static com.ukao.cashregister.R.id.user_info_credits;
import static com.ukao.cashregister.R.id.user_info_part5_group_tv;
import static com.ukao.cashregister.R.id.user_info_part5_vip_levels_tv;
import static com.ukao.cashregister.R.id.user_info_part6_tag1;
import static com.ukao.cashregister.R.id.user_info_subscribe;
import static com.ukao.cashregister.R.id.user_info_vip_card_no;
import static com.ukao.cashregister.R.id.user_info_wechat_name;
import static com.ukao.cashregister.utils.MyDateUtils.getTime;


/**
 * 客户信息
 */
public class UserInfoFragment extends MvpFragment<UserInfoPesenter> implements UserInfoView {

    Unbinder unbinder;
    @BindView(R.id.back_layout)
    LinearLayout backLayout;
    @BindView(R.id.user_info_icon)
    ShapedImageView userInfoIcon;
    @BindView(R.id.user_info_name)
    TextView userInfoName;
    @BindView(user_info_wechat_name)
    TextView userInfoWechatName;
    @BindView(R.id.wxId)
    ImageView wxId;
    @BindView(R.id.phone)
    ImageView phone;
    @BindView(R.id.verifyAndriod)
    ImageView verifyAndriod;
    @BindView(R.id.verifyIos)
    ImageView verifyIos;
    @BindView(R.id.verifyPhone)
    ImageView verifyPhone;
    @BindView(user_info_subscribe)
    TextView userInfoSubscribe;
    @BindView(user_info_balance)
    TextView userInfoBalance;
    @BindView(user_info_credits)
    TextView userInfoCredits;
    @BindView(R.id.user_info_coupons)
    TextView userInfoCoupons;
    @BindView(R.id.user_info_coupons_layout)
    LinearLayout userInfoCouponsLayout;
    @BindView(R.id.user_info_phone_account)
    TextView userInfoPhoneAccount;
    @BindView(R.id.user_info_phone_is_bind)
    ImageView userInfoPhoneIsBind;
    @BindView(user_info_vip_card_no)
    TextView userInfoVipCardNo;
    @BindView(R.id.user_info_vip_card_is_bind)
    ImageView userInfoVipCardIsBind;
    @BindView(R.id.user_info_part3_vip_layout)
    LinearLayout userInfoPart3VipLayout;
    @BindView(R.id.user_info_part4_name_tv)
    TextView userInfoPart4NameTv;
    @BindView(R.id.user_info_part4_name_layout)
    LinearLayout userInfoPart4NameLayout;
    @BindView(R.id.sex)
    TextView sex;
    @BindView(R.id.user_info_part4_sex_layout)
    LinearLayout userInfoPart4SexLayout;
    @BindView(R.id.user_info_part4_birth_tv)
    TextView userInfoPart4BirthTv;
    @BindView(R.id.user_info_part4_birth_layout)
    LinearLayout userInfoPart4BirthLayout;
    @BindView(user_info_part5_vip_levels_tv)
    TextView userInfoPart5VipLevelsTv;
    @BindView(R.id.user_info_part5_vip_discount)
    TextView userInfoPart5VipDiscount;
    @BindView(R.id.user_info_part5_vip_levels_layout)
    LinearLayout userInfoPart5VipLevelsLayout;
    @BindView(user_info_part5_group_tv)
    TextView userInfoPart5GroupTv;
    @BindView(R.id.user_info_part5_group_layout)
    LinearLayout userInfoPart5GroupLayout;
    @BindView(user_info_part6_tag1)
    TextView userInfoPart6Tag1;
    @BindView(R.id.user_info_part6_tag2)
    TextView userInfoPart6Tag2;
    @BindView(R.id.user_info_part6_tag3)
    TextView userInfoPart6Tag3;
    @BindView(R.id.user_info_part4_age_tv)
    TextView userInfoPart4AgeTv;
    @BindView(R.id.user_info_part6_tag_layout)
    LinearLayout userInfoPart6TagLayout;
    @BindView(R.id.user_info_right_container)
    FrameLayout userInfoRightContainer;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.btn_orders_btn)
    Button ordersBtn;  //下单按钮
    @BindView(R.id.tv_union_name)
    TextView tvUnionName;

    private TimePickerView pvCustomTime;
    //判断当前 Fragment属于下单或取衣
    private String classType = "";
    private String timeType = "yyyy年 MM月 dd日";
    private UserInfoBean userInfoData;
    private String userId ="";
    private ArrayList<Coupons> mCoupons;
    private String title;
    private UserInfoBean.UserBean useIdParam;
    private UserInfoBean.UserBean.CardBean mCardBean;
    private ArrayList<ChooseTag.ChooseTaglist> tagAllList;
    private List<ChooseTag.ChooseTaglist> tags;
    private List<RechargeBean> mRechargeList;
    private boolean binding;
    private VipCardDialogFragment mVipCardDialog;
    private UserInfoBean.UserBean.CardBean cardItem;

    private String rfidSecret;
    private String nfcRfidSecret;
    private ClientGroupingBean clientGroupingBean;


    public static UserInfoFragment newInstance(String param1, Serializable userInfoData) {
        UserInfoFragment fragment = new UserInfoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putSerializable(ARG_PARAM2, userInfoData);
        fragment.setArguments(args);
        return fragment;
    }

    public static UserInfoFragment newInstance(String param1, Serializable useParam, String title) {
        UserInfoFragment fragment = new UserInfoFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putSerializable(ARG_PARAM2, useParam);
        args.putString(ARG_PARAM3, title);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            classType = getArguments().getString(ARG_PARAM1);
            Serializable mSerializable = getArguments().getSerializable(ARG_PARAM2);
            title = getArguments().getString(ARG_PARAM3, "");
            if (mSerializable instanceof UserInfoBean) {
                userInfoData = (UserInfoBean) mSerializable;
            } else {
                useIdParam = (UserInfoBean.UserBean)mSerializable ;
            }

        }
    }

    @Override
    protected UserInfoPesenter createPresenter() {
        return new UserInfoPesenter(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_info, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }




    @Override
    protected void initView() {

        if (!CheckUtils.isNull(userInfoData)) {
            fillUserData(userInfoData.getUser());
        } else {
            toolbarTitle.setText(title);
            fillUserData(useIdParam);
            //  mvpPresenter.userinfoData(_mActivity,useIdParam.getInfo().getId());
        }
    }

    private void fillUserData(UserInfoBean.UserBean mitem) {

        UserInfoBean.UserBean.InfoBeanX useInfo = mitem.getInfo();
        //设置客户是否 是团体客服
        if(mitem.getUnionInfo()!=null){
            UserInfoBean.UserBean.UnionInfoBean unionInfo=mitem.getUnionInfo();
            tvUnionName.setVisibility(View.VISIBLE);
            tvUnionName.setText(unionInfo.getName());
        }else {
            tvUnionName.setText("");
            tvUnionName.setVisibility(View.INVISIBLE);
        }
        mCardBean = mitem.getCard();
        userInfoName.setText(useInfo.getName());/*设置用户名*/
        userId = useInfo.getId();
        userInfoPhoneAccount.setText(!CheckUtils.isEmpty(useInfo.getPhone()) == true ? useInfo.getPhone() : "");/*手机*/
        userInfoSubscribe.setVisibility(useInfo.getSubscribe() == true ? (View.VISIBLE) : (View.GONE)); /*微信已跑路*/
        wxId.setVisibility(CheckUtils.isEmptyNullNumber(useInfo.getWxId()) == 1 ? (View.VISIBLE) : (View.GONE));
        phone.setVisibility(!CheckUtils.isEmpty(useInfo.getPhone()) == true ? (View.VISIBLE) : (View.GONE));
        verifyPhone.setVisibility(CheckUtils.isEmptyNullNumber(useInfo.getVerifyPhone()) == 1 ? (View.VISIBLE) : (View.GONE));
        userInfoPhoneIsBind.setImageResource(CheckUtils.isEmptyNullNumber(useInfo.getVerifyPhone()) == 1 ? R.drawable.user_info_phone_bind : R.color.transparent);
        verifyAndriod.setVisibility(CheckUtils.isEmptyNullNumber(useInfo.getVerifyAndriod()) == 1 ? (View.VISIBLE) : (View.GONE));
        verifyIos.setVisibility(CheckUtils.isEmptyNullNumber(useInfo.getVerifyIos()) == 1 ? (View.VISIBLE) : (View.GONE));
        userInfoWechatName.setText(CheckUtils.isEmpty(useInfo.getWxNickname()) == true ? "" : "微信昵称：" + useInfo.getWxNickname());
        ImageUtils.loadImage(_mActivity, useInfo.getHeadimgPath(), userInfoIcon);
        userInfoPart4NameTv.setText(!CheckUtils.isEmpty(useInfo.getName()) == true ? useInfo.getName() : "");
        String couponCnt = CheckUtils.isEmptyString(mitem.getCouponCnt());
        userInfoCoupons.setText((CheckUtils.isEmpty(couponCnt) ? "0" : couponCnt) + "张");

        UserInfoBean.UserBean.ExtBean extItem = mitem.getExt();
        if (!CheckUtils.isNull(extItem)) {
            userInfoCredits.setText(extItem.getPoint() + "");  /* 积分*/
            userInfoBalance.setText("￥" + CheckUtils.isEmptyNumber(extItem.getBalance()));
            try {
                if (!CheckUtils.isEmpty(extItem.getBirthday())) {
                    Date dataBirthday = MyDateUtils.stringDate(extItem.getBirthday(), "yyyy-MM-dd");
                    userInfoPart4BirthTv.setText(MyDateUtils.getTime(dataBirthday, timeType));  /*生日*/
                    userInfoPart4AgeTv.setText(MyDateUtils.getAge(dataBirthday) + "岁");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        sex.setText(useInfo.getSex());  //设置性别
        cardItem = mitem.getCard();
        if (!CheckUtils.isNull(cardItem)) {
            binding = cardItem.getIsBinding();
            userInfoVipCardNo.setText(!CheckUtils.isEmpty(cardItem.getNo())? cardItem.getNo() : "");  /*卡号*/
            userInfoPart5VipLevelsTv.setText(!CheckUtils.isEmpty(cardItem.getInfo().getName())? cardItem.getInfo().getName() : "");  /*卡号*/
            userInfoPart5VipDiscount.setText(cardItem.getInfo().getDiscount());
        }

        userInfoVipCardIsBind.setImageResource(binding ? R.drawable.bind_card : R.drawable.unbind_card);
        UserInfoBean.UserBean.GroupBean mUserGroupItem = mitem.getGroup();
        if (mUserGroupItem != null) {
            userInfoPart5GroupTv.setText(!CheckUtils.isEmpty(mUserGroupItem.getName()) ? mUserGroupItem.getName() : "");  /*分组*/
            clientGroupingBean=new ClientGroupingBean();
            clientGroupingBean.setId(mUserGroupItem.getId());//获得选中分组ID
            clientGroupingBean.setName(userInfoPart5GroupTv.getText().toString());//获得选中分组名字
        }
        List<UserInfoBean.UserBean.TagListBean> mTagData = mitem.getTagList(); /*标签集合*/
        if (!CheckUtils.isEmpty(mTagData)) {
            int size = 0;
            size = mTagData.size() > 3 ? 3 : mTagData.size();
            for (int i = 0; i < size; i++) {
                UserInfoBean.UserBean.TagListBean.UserTagBean mTagitem = mTagData.get(i).getUserTag();
                if (!CheckUtils.isNull(mTagitem)) {
                    switch (i) {
                        case 0:
                            userInfoPart6Tag1.setText(mTagitem.getName());
                            userInfoPart6Tag1.setVisibility(View.VISIBLE);
                            break;
                        case 1:
                            userInfoPart6Tag2.setText(mTagitem.getName());
                            userInfoPart6Tag2.setVisibility(View.VISIBLE);
                            break;
                        case 2:
                            userInfoPart6Tag3.setText(mTagitem.getName());
                            userInfoPart6Tag3.setVisibility(View.VISIBLE);
                            break;
                    }
                }
            }
        }
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        switch (classType) {
            case GlobalConsts.CREATE_ORDERS:  //下单的
                if (!CheckUtils.isEmpty(userId)){
                    replaceLoadRootFragment(R.id.user_info_right_container,
                            CreateOrdersUserInfoRightFragment.newInstance(getText(userInfoPhoneAccount), getText(userInfoName), userId, getText(sex)), false);
                }
                break;
            case GlobalConsts.PICK_CLOTHING:  //取衣
                ordersBtn.setVisibility(View.VISIBLE);
                if (!CheckUtils.isNull(userInfoData))
                    replaceLoadRootFragment(R.id.user_info_right_container, PickClothingUserInfoRightFragment.newInstance(false, userInfoData.getSearchValue(), userInfoData.getUser().getInfo().getId()), false);
                break;
            case GlobalConsts.VIP_DETAILS:   //会员详情
                ordersBtn.setVisibility(View.VISIBLE);
                if (!CheckUtils.isNull(useIdParam))
                    replaceLoadRootFragment(R.id.user_info_right_container, VipDetailsTabFragment.newInstance(classType, useIdParam), false);
                break;
        }


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
        MembershipCardHelper.getInstance().onDestroy();
    }


    @OnClick({R.id.back_layout, R.id.user_info_coupons_layout, R.id.user_info_part3_vip_layout, R.id.recharge_btn,
            R.id.user_info_part4_sex_layout, R.id.user_info_part4_name_layout, R.id.user_info_part4_birth_layout,
            R.id.user_info_part5_vip_levels_layout, R.id.user_info_part6_tag_layout,
            R.id.user_info_part5_group_layout,R.id.btn_orders_btn})
    public void onViewClicked(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.recharge_btn:  //充值
                if (mRechargeList == null)
                    mvpPresenter.queryRechargeList(_mActivity);
                else {
                    start(GatheringFragment.newInstance((ArrayList<RechargeBean>) mRechargeList, CheckUtils.isNull(useIdParam) ? userInfoData.getUser() : useIdParam));
                }
                break;
            case R.id.back_layout:
                //如果右边显示在添加地址页面，则需先关闭右侧界面
                UserInfoEvent.postNoData(UserInfoEvent.Message.CLOSE);
                finish();
                break;
            case R.id.user_info_coupons_layout:   //打开优惠卷窗口
                if (CheckUtils.isEmpty(mCoupons)) {
                    mvpPresenter.couponInfoList(userId);
                } else {
                    showCouponsDialog();
                }
                break;
            case R.id.user_info_part3_vip_layout: /*绑卡*/
                showVipCardDialog(bundle);
                break;
            case R.id.user_info_part4_sex_layout: //打开性别选择窗口
                EditSexDialogFragment editSex = new EditSexDialogFragment();
                bundle.putString("sex", getText(sex));
                editSex.setArguments(bundle);
                editSex.show(getChildFragmentManager(), EditSexDialogFragment.class.getName());
                break;
            case R.id.user_info_part4_name_layout:  //打开修改姓名的窗口
                EditNameDialogFragment editName = new EditNameDialogFragment();
                bundle.putString("name", userInfoPart4NameTv.getText().toString());
                editName.setArguments(bundle);
                editName.show(getChildFragmentManager(), EditNameDialogFragment.class.getName());
                break;
            case R.id.user_info_part4_birth_layout:
                String birthday = userInfoPart4BirthTv.getText().toString();
                DialogTool.getSingleton().showDatePicker(birthday, timeType, activity, mOnTimeSelectListener);
                break;
            case R.id.user_info_part5_vip_levels_layout:
                break;
            case R.id.user_info_part6_tag_layout:
                mvpPresenter.userTagAlllist();
                break;
            case R.id.user_info_part5_group_layout:  //打开分组选择窗口
                clientGroupingBean.setSelected(true);
                ClientGroupingDialogFragment fragment =ClientGroupingDialogFragment.newInstance(clientGroupingBean,userId);
                fragment.setOnDialogSureClickListener(onDialogSureClickListener);
                fragment.show(getChildFragmentManager(),ClientGroupingDialogFragment.class.getName());
                break;
            case R.id. btn_orders_btn:  //客服信息左边的下单按钮
                ordersBtn.setVisibility(View.INVISIBLE);//隐藏按钮
                if (!CheckUtils.isEmpty(userId)){
                    replaceLoadRootFragment(R.id.user_info_right_container,
                            CreateOrdersUserInfoRightFragment.newInstance(getText(userInfoPhoneAccount), getText(userInfoName), userId, getText(sex)), false);
                }
                break;
        }
    }

    OnDialogSureClickListener onDialogSureClickListener=new OnDialogSureClickListener() {
        @Override
        public void onSureItemClick(Object message) {
            clientGroupingBean= (ClientGroupingBean)message;
            if(!CheckUtils.isEmpty(clientGroupingBean.getName())){
                userInfoPart5GroupTv.setText(clientGroupingBean.getName());  /*分组*/
            }
        }
    };

    /**
     * 绑定卡对话框
     *
     * @param bundle
     */
    private void showVipCardDialog(Bundle bundle) {
        if (CheckUtils.isNull(mCardBean)) return;
        L.i("binding=" + binding);
        mVipCardDialog = new VipCardDialogFragment();
        bundle.putBoolean("binding", binding);
        bundle.putSerializable("cardBean", mCardBean);
        mVipCardDialog.setArguments(bundle);
        mVipCardDialog.show(getChildFragmentManager(), EditSexDialogFragment.class.getName());
    }

    /**
     * 优惠券
     */
    private void showCouponsDialog() {
        CouponsDialogFragment coupons = new CouponsDialogFragment();
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(ARG_PARAM1, mCoupons);
        coupons.setArguments(mBundle);
        coupons.show(getChildFragmentManager(), CouponsDialogFragment.class.getName());
    }


    private TimePickerView.OnTimeSelectListener mOnTimeSelectListener = new TimePickerView.OnTimeSelectListener() {
        @Override
        public void onTimeSelect(Date date, View v) {

            mvpPresenter.updateBirthday(_mActivity, userId, MyDateUtils.formatDate(date), getText(userInfoPart4BirthTv));
            userInfoPart4BirthTv.setText(getTime(date, timeType));

            try {
                userInfoPart4AgeTv.setText(MyDateUtils.getAge(date) + "岁");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };


    @Override
    protected boolean useEventBus() {
        return true;
    }

    @Subscribe
    public void onMembershipCardEvent(MembershipCardEvent event) {
        switch (event.getMessage()) {
            case fali:/*读卡失败*/
                if (!isVisible) return;
                String error = (String) event.getData();
                T.show(error);
                break;
            case add:/*读卡成功  绑定卡*/
                if (!isVisible) return;
                if (!CheckUtils.isNull(mVipCardDialog) && mVipCardDialog.getDialog().isShowing()) {
                    mVipCardDialog.dismiss();
                }
               String rfidSecret= (String) event.getData();
                mvpPresenter.updateCard(userId, rfidSecret);
                break;
            case notFound:/*没有卡*/
                if (!isVisible) return;
                ThreadUtils.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        T.show("必须先将卡放上去，再点绑卡");
                    }
                });

                break;
            case   nfc_read:  /*商米NFC读卡*/
                nfcRfidSecret= (String) event.getData();
                break;

        }
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        isVisible=true;


    }

    @Override
    public void onSupportInvisible() {
        super.onSupportInvisible();
        isVisible=false;
        nfcRfidSecret=null;
    }





    @Subscribe
    public void onEvent(UserInfoEvent event) {
        switch (event.getMessage()) {
            case NAME:
                String name = (String) event.getData();
                userInfoPart4NameTv.setText(name);
                userInfoName.setText(name);
                mvpPresenter.updateName(_mActivity, userId, name);
                break;
            case VIP:
                break;
            case BIRTH:
                break;
            case TAG:
                tags = (List<ChooseTag.ChooseTaglist>) event.getData();
                JSONArray mJSONArray = new JSONArray();
                for (int i = 0; i < tags.size() - 1; i++) {
                    ChooseTag.ChooseTaglist mtagItem = tags.get(i);
                    JSONObject mJSONObject = new JSONObject();
                    try {
                        mJSONObject.put("id", mtagItem.getId());
                        mJSONObject.put("name", mtagItem.getName());
                        mJSONArray.put(mJSONObject);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                mvpPresenter.updateTag(userId, mJSONArray.toString());

                break;
            case SEX:/*性别*/
                String strSex = (String) event.getData();
                String selectsex = "";
                switch (strSex) {
                    case "未知":
                        selectsex = "0";
                        break;
                    case "男":
                        selectsex = "1";
                        break;
                    case "女":
                        selectsex = "2";
                        break;
                }
                sex.setText(strSex);
                mvpPresenter.updateSex(_mActivity, userId, selectsex, strSex);
                break;
            case CODEBING:/*验证码绑定*/
                Map mMap = (Map) event.getData();
                String rfidNo = (String) mMap.get(ARG_PARAM1);
                String checkCode = (String) mMap.get(ARG_PARAM2);
                mvpPresenter.codebinding(userId, rfidNo, checkCode);
                break;

            case BING:/*对话框绑定*/
            /* NFC 读出来的卡*/
                if (!CheckUtils.isEmpty(nfcRfidSecret)){
                    mvpPresenter.updateCard(userId, nfcRfidSecret);
                    nfcRfidSecret=null;
                }else{
                    MembershipCardHelper.getInstance().readUserId();
                }

                break;
            case UNBING: /*对话框解绑*/
                mvpPresenter.unbundling(userId);
                break;
            case USEINFO:/*获取用户信息*/
                mvpPresenter.userinfoData(userId);
                break;
            case LOSSCARD:  /*作废*/
                mvpPresenter.cancel(userId);
                break;


            case PAYPAGE:  /*支付界面*/

                String ordid = (String) event.getData();
                if (CheckUtils.isEmpty(ordid)) return;

                start(GatheringFragment.newInstance(ordid, CheckUtils.isEmpty(title) ? "返回客户信息" : "返回" + title));
                break;
            case CLOTHINDEVICE:/*衣物详情*/
                ClothginQuiryBean mClothginQuiryBean = (ClothginQuiryBean) event.getData();
                if (!CheckUtils.isNull(mClothginQuiryBean)) {

                    start(ClothingDetailsFragment.newInstance(mClothginQuiryBean), SINGLETASK);
                }

                break;


        }
    }


    /**
     * 绑卡/解卡 成功对话框
     */
    private void bingCardSucceedDialog(String hide) {
        final BindingDialogFragment bingDialog = new BindingDialogFragment();
        Bundle mBundle = new Bundle();
        mBundle.putString(ARG_PARAM1, hide);
        bingDialog.setArguments(mBundle);
        bingDialog.show(getChildFragmentManager(), BindingDialogFragment.class.getName());
        backLayout.postDelayed(new Runnable() {
            @Override
            public void run() {

                bingDialog.dismiss();
            }
        }, 500);
    }

    @Override
    public void userInfoSucceed(UserInfoBean.UserBean mUserInfoBean) {
        fillUserData(mUserInfoBean);
    }

    @Override
    public void loadFail(String message) {
        T.show(message);
    }

    @Override
    public void updateSexFail(String error, String strsex) {
        T.show(error);
        sex.setText(strsex);
    }

    @Override
    public void updateBirthdayFail(String error, String birthday) {
        T.show(error);
        userInfoPart4BirthTv.setText(birthday);
    }

    @Override
    public void updateTagSucceed() {
        //由于手动添加了一个“添加标签”字段，所以这里需要判断多一位
        userInfoPart6Tag1.setVisibility(View.GONE);
        userInfoPart6Tag2.setVisibility(View.GONE);
        userInfoPart6Tag3.setVisibility(View.GONE);
        if (tags.size() > 1) {
            userInfoPart6Tag1.setVisibility(View.VISIBLE);
            userInfoPart6Tag1.setText(tags.get(0).getName());
        }
        if (tags.size() > 2) {
            userInfoPart6Tag2.setVisibility(View.VISIBLE);
            userInfoPart6Tag2.setText(tags.get(1).getName());
        }
        if (tags.size() > 3) {
            userInfoPart6Tag3.setVisibility(View.VISIBLE);
            userInfoPart6Tag3.setText(tags.get(2).getName());
        }
    }

    @Override
    public void updateNameFail(String error, String name) {
        T.show(error);
        userInfoPart4NameTv.setText(name);
        userInfoName.setText(name);
    }


    /**
     * 获取优惠券列表
     *
     * @param mCoupons
     */
    @Override
    public void receiveCouponsData(List<Coupons> mCoupons) {
        this.mCoupons = (ArrayList<Coupons>) mCoupons;
        showCouponsDialog();


    }


    @Override
    public void loadAllTagData(List<ChooseTag.ChooseTaglist> mCoupons) {
        tagAllList = (ArrayList) mCoupons;
        mvpPresenter.userTaglist(userId);

    }

    @Override
    public void loadUseTagData(List<ChooseTag.ChooseTaglist> mCoupons) {
        ChooseTagDialogFragment tag = new ChooseTagDialogFragment();
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(ARG_PARAM1, tagAllList);
        mBundle.putSerializable(ARG_PARAM2, (ArrayList) mCoupons);
        tag.setArguments(mBundle);
        tag.show(getChildFragmentManager(), ChooseTagDialogFragment.class.getName());
    }

    @Override
    public void rechargeSucceed(String msg) {
        T.show(msg);
        mvpPresenter.userinfoData(userId);
    }

    @Override
    public void rechargeTableSucceed(List<RechargeBean> mRechargeList) {
        this.mRechargeList = mRechargeList;
        start(GatheringFragment.newInstance((ArrayList<RechargeBean>) mRechargeList, CheckUtils.isNull(useIdParam) ? userInfoData.getUser() : useIdParam));
    }

    /**
     * 绑卡成功
     *
     * @param msg
     */
    @Override
    public void updateCardSucceed(String msg, String rfidNo) {
        binding = true;
        cardItem.setRfidNo(rfidNo);
        userInfoVipCardNo.setText(CheckUtils.isEmptyString(rfidNo));
        Dialog mDialog = mVipCardDialog.getDialog();
        if (!CheckUtils.isNull(mDialog) && mVipCardDialog.getDialog().isShowing()) {
            mVipCardDialog.dismiss();
        }
        userInfoVipCardIsBind.setImageResource(binding ? R.drawable.bind_card : R.drawable.unbind_card);
        bingCardSucceedDialog(msg);
    }

    /**
     * 解绑卡成功
     *
     * @param msg
     */
    @Override
    public void unbindCardSucceed(String msg) {
        binding = false;
        cardItem.setRfidNo("");
        userInfoVipCardNo.setText("");
        userInfoVipCardIsBind.setImageResource(binding ? R.drawable.bind_card : R.drawable.unbind_card);
        bingCardSucceedDialog(msg);
    }
    /**
     * 作废
     *
     * @param msg
     */

    @Override
    public void lossSucceed(String msg) {
        binding = false;
        cardItem.setRfidNo("");
        userInfoVipCardNo.setText("");
        userInfoVipCardIsBind.setImageResource(binding ? R.drawable.bind_card : R.drawable.unbind_card);
        bingCardSucceedDialog(msg);
    }


}
